﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.IO;
using System.Drawing.Imaging;

namespace ru.yourpictures.Core.Imaging.Filters
{
    
    public class Helper
    {
        /// <summary>
        /// Proportionally squeezes image when proportional flag set to true.
        /// </summary>
        /// <param name="originalWidth"></param>
        /// <param name="originalHeight"></param>
        /// <param name="proportional"></param>
        /// <param name="newWidth"></param>
        /// <param name="newHeight"></param>
        /// <param name="keepOrientation">When true, image will be resized exactly using new width and height. 
        /// Otherwise new rectangle will be oriented the same way as image.
        /// </param>
        /// <returns></returns>
        public static Rectangle CalculateImageRectangle(int originalWidth, int originalHeight, bool proportional, int newWidth, int newHeight, bool keepOrientation)
        {
            Rectangle result = new Rectangle();

            if (!proportional)
            {
                result.Width = newWidth;
                result.Height = newHeight;
                return result;
            }

            if (newWidth <= 0) newWidth = originalWidth;
            if (newHeight <= 0) newHeight = originalHeight;

            if (!keepOrientation)
            {
                if (
                    (originalWidth > originalHeight && newWidth < newHeight) 
                    ||
                    (originalWidth < originalHeight && newWidth > newHeight)
                    )
                {
                    int tmp = newWidth;
                    newWidth = newHeight;
                    newHeight = tmp;
                }
            }

            decimal wRelation = (decimal)originalWidth / (decimal)newWidth;
            decimal hRelation = (decimal)originalHeight / (decimal)newHeight;
            decimal propRelation = Math.Max(wRelation, hRelation);

            result.Height = (int)((decimal)originalHeight / propRelation);
            result.Width = (int)((decimal)originalWidth / propRelation);
            return result;
        }

        #region CompressJpg
        /// <summary>
        /// Compress Jpeg image
        /// </summary>
        /// <param name="original">Image to be compressed</param>
        /// <param name="compression">Value between 1 and 100. 100 - no compression, 1 - maximal compression.</param>
        /// <returns></returns>
        public static MemoryStream CompressJpg(System.Drawing.Image original, long compression)
        {
            MemoryStream ms = new MemoryStream();
            EncoderParameters eps = new EncoderParameters(1);
            eps.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, compression);

            ImageCodecInfo ici = GetEncoderInfo("image/jpeg");
            original.Save(ms, ici, eps);
            return ms;
        }
        #endregion

        #region GetEncoderInfo
        /// <summary>
        /// See KB article Q324790
        /// </summary>
        /// <param name="mimeType">String parameter - MIME type, KB Q324790</param>
        /// <returns></returns>
        public static ImageCodecInfo GetEncoderInfo(String mimeType)
        {
            int j;
            ImageCodecInfo[] encoders;
            encoders = ImageCodecInfo.GetImageEncoders();
            for (j = 0; j < encoders.Length; ++j)
            {
                if (encoders[j].MimeType == mimeType)
                    return encoders[j];
            }
            return null;
        }
        #endregion

        public static ColorPalette GetTransparentColorPalette(Bitmap original)
        {
            Color testColor;
            Color newColor;
            ColorPalette pal = original.Palette;

            for (int i = 0; i < pal.Entries.Length - 1; i++)
            {
                testColor = pal.Entries[i];

                if (testColor.A == 0)
                {
                    newColor = Color.FromArgb(0, testColor);
                    pal.Entries[i] = newColor;

                }
            }

            return pal;
        }

        public static byte[] ImageToByteArray(System.Drawing.Image original, ImageFormat format)
        {
            return ImageToMemoryStream(original, format).GetBuffer();
        }

        public static MemoryStream ImageToMemoryStream(System.Drawing.Image original, ImageFormat format)
        {
            MemoryStream ms = new MemoryStream();
            original.Save(ms, format);
            return ms;
        }


    }
}
